home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Menús / OwnerDrawMenu / OwnerDrawMenu.cs next >
Encoding:
Text File  |  2002-06-19  |  4.5 KB  |  128 lines

  1. //--------------------------------------------
  2. // OwnerDrawMenu.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Text;    // Para la enumeraci≤n HotkeyPrefix
  7. using System.Windows.Forms;
  8.  
  9. class OwnerDrawMenu: Form
  10. {
  11.      const    int  iFontPointSize = 18; // Para los elementos de men·
  12.      MenuItem      miFacename;
  13.  
  14.      public static void Main()
  15.      {
  16.           Application.Run(new OwnerDrawMenu());
  17.      }
  18.      public OwnerDrawMenu()
  19.      {
  20.           Text = "Men· dibujado por el propietario";
  21.  
  22.                // Elementos de nivel superior
  23.           
  24.           Menu = new MainMenu();
  25.           Menu.MenuItems.Add("&Nombres de fuente");
  26.  
  27.                // Matriz de elementos del submen·
  28.  
  29.           string[] astrText = {"&Times New Roman", "&Arial", "&Courier New"};
  30.           MenuItem [] ami = new MenuItem[astrText.Length];
  31.  
  32.           EventHandler ehOnClick = new EventHandler(MenuFacenameOnClick);
  33.           MeasureItemEventHandler ehOnMeasureItem = 
  34.                     new MeasureItemEventHandler(MenuFacenameOnMeasureItem);
  35.           DrawItemEventHandler ehOnDrawItem = 
  36.                     new DrawItemEventHandler(MenuFacenameOnDrawItem);
  37.  
  38.           for (int i = 0; i < ami.Length; i++)
  39.           {
  40.                ami[i]              = new MenuItem(astrText[i]);
  41.                ami[i].OwnerDraw    = true;
  42.                ami[i].RadioCheck   = true;
  43.                ami[i].Click       += ehOnClick;
  44.                ami[i].MeasureItem += ehOnMeasureItem;
  45.                ami[i].DrawItem    += ehOnDrawItem;
  46.           }
  47.           miFacename = ami[0];
  48.           miFacename.Checked = true;
  49.  
  50.           Menu.MenuItems[0].MenuItems.AddRange(ami);
  51.      }
  52.      void MenuFacenameOnClick(object obj, EventArgs ea)
  53.      {
  54.           miFacename.Checked = false;
  55.           miFacename = (MenuItem) obj;
  56.           miFacename.Checked = true;
  57.  
  58.           Invalidate();
  59.      }
  60.      void MenuFacenameOnMeasureItem(object obj, MeasureItemEventArgs miea)
  61.      {
  62.           MenuItem mi = (MenuItem) obj;
  63.           Font font = new Font(mi.Text.Substring(1), iFontPointSize);
  64.  
  65.           StringFormat strfmt = new StringFormat();
  66.           strfmt.HotkeyPrefix = HotkeyPrefix.Show;
  67.  
  68.           SizeF sizef = miea.Graphics.MeasureString(mi.Text, font, 
  69.                                                     1000, strfmt);
  70.  
  71.           miea.ItemWidth  = (int) Math.Ceiling(sizef.Width);
  72.           miea.ItemHeight = (int) Math.Ceiling(sizef.Height);
  73.  
  74.           miea.ItemWidth += SystemInformation.MenuCheckSize.Width * 
  75.                               miea.ItemHeight / 
  76.                                    SystemInformation.MenuCheckSize.Height;
  77.  
  78.           miea.ItemWidth -= SystemInformation.MenuCheckSize.Width;
  79.      }
  80.      void MenuFacenameOnDrawItem(object obj, DrawItemEventArgs diea) 
  81.      {
  82.           MenuItem mi   = (MenuItem) obj;
  83.           Graphics grfx = diea.Graphics;
  84.           Brush    brush;
  85.  
  86.                // Crea la fuente y el formato de la cadena.
  87.  
  88.           Font font = new Font(mi.Text.Substring(1), iFontPointSize);
  89.           StringFormat strfmt = new StringFormat();
  90.           strfmt.HotkeyPrefix = HotkeyPrefix.Show;
  91.  
  92.                // Calcula los rectßngulos de marca de verificaci≤n y texto.
  93.  
  94.           Rectangle rectCheck = diea.Bounds;
  95.  
  96.           rectCheck.Width = SystemInformation.MenuCheckSize.Width * 
  97.                               rectCheck.Height / 
  98.                                    SystemInformation.MenuCheckSize.Height;
  99.  
  100.           Rectangle rectText = diea.Bounds;
  101.  
  102.           rectText.X += rectCheck.Width;
  103.  
  104.                // Realiza todo el dibujo.
  105.  
  106.           diea.DrawBackground();
  107.  
  108.           if ((diea.State & DrawItemState.Checked) != 0)
  109.                ControlPaint.DrawMenuGlyph(grfx, rectCheck, MenuGlyph.Bullet);
  110.           
  111.           if ((diea.State & DrawItemState.Selected) != 0)
  112.                brush = SystemBrushes.HighlightText;
  113.           else
  114.                brush = SystemBrushes.FromSystemColor(SystemColors.MenuText);
  115.  
  116.           grfx.DrawString(mi.Text, font, brush, rectText, strfmt);
  117.      }
  118.      protected override void OnPaint(PaintEventArgs pea)
  119.      {
  120.           Graphics grfx = pea.Graphics;
  121.           Font font = new Font(miFacename.Text.Substring(1), 12);
  122.           StringFormat strfmt = new StringFormat();
  123.           strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
  124.  
  125.           grfx.DrawString(Text, font, new SolidBrush(ForeColor), 0, 0);
  126.      }
  127. }
  128.